<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    /* 
      https://leetcode-cn.com/problems/jian-sheng-zi-lcof/
      思路：https://leetcode-cn.com/problems/jian-sheng-zi-lcof/solution/shuang-jie-fa-dong-tai-gui-hua-tan-xin-fa-fu-zha-2/
            https://leetcode-cn.com/problems/jian-sheng-zi-ii-lcof/solution/jian-sheng-zi-ii-shu-xue-gui-lu-de-zui-you-jie-by-/
     */

    // 1.动态规划
    function cuttingRope(n) {
      const dp = new Array(n + 1).fill(1)

      for (let i = 3; i <= n; i++) {
        for (let j = 0; j < i; j++) {
          dp[i] = Math.max(dp[i], (i - j) * j, dp[i - j] * j)
        }
      }

      return dp[n]
    }

    // 2.贪心思想
    function cuttingRope(n) {
      if (n === 2) return 1;
      if (n === 3) return 2;

      let a = n / 3 | 0;
      let b = n % 3;

      if (b === 0) return Math.pow(3, a);
      if (b === 1) return Math.pow(3, a - 1) * 4;
      if (b === 2) return Math.pow(3, a) * 2;
    }

    // 3.利用数学规律
    function cuttingRope(n) {
      let arr =[0, 0, 1, 2, 4]
      if (n < 5)  return arr[n]
      let res = 1
      while (n >= 5) {
        res = res * 3
        n -= 3
      }
      return res * n
    }
  </script>
</body>
</html>